//
//  SetLoginPswViewController.m
//  Kjs-iOS
//
//  Created by 江伟 on 16/6/21.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "SetLoginPswViewController.h"
#import "AppDelegate.h"
#import "KJSRequest.h"
#import "CommonUtil.h"

@interface SetLoginPswViewController ()
@property (nonatomic,weak) IBOutlet UIButton *submit;
@property (nonatomic,weak) IBOutlet UITextField *oldPswTF;
@property (nonatomic,weak) IBOutlet UITextField *PswTF;
@property (nonatomic,weak) IBOutlet UITextField *confirmNewPswTF;

@property (weak, nonatomic) IBOutlet UIButton *oldPswEyeBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmPswEyeBtn;
@property (weak, nonatomic) IBOutlet UIButton *pswNewEyeBtn;

@property (strong,nonatomic) NSString *tempStr;
@end

@implementation SetLoginPswViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"设置登录密码"];
    self.submit.layer.cornerRadius = 10 ;
    [self.submit addTarget:self action:@selector(AlterLoginPassword) forControlEvents:UIControlEventTouchUpInside];
    self.oldPswTF.secureTextEntry = YES;
    self.PswTF.secureTextEntry = YES;
    self.confirmNewPswTF.secureTextEntry = YES;
    
    self.oldPswEyeBtn.tag = 1;
    [self.oldPswEyeBtn addTarget:self action:@selector(swichPasswordVisiable:) forControlEvents:UIControlEventTouchUpInside];
    self.confirmPswEyeBtn.tag = 2;
    [self.confirmPswEyeBtn addTarget:self action:@selector(swichPasswordVisiable:) forControlEvents:UIControlEventTouchUpInside];
    self.pswNewEyeBtn.tag = 3;
    [self.pswNewEyeBtn addTarget:self action:@selector(swichPasswordVisiable:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - custom method
-(void)swichPasswordVisiable:(id)sender
{
    UIButton *eyeBtn = (UIButton *)sender;
    switch (eyeBtn.tag) {
        case 1:
        {
            self.tempStr = self.oldPswTF.text;
            self.oldPswTF.text = @"";
            self.oldPswTF.secureTextEntry = !self.oldPswTF.secureTextEntry;
            self.oldPswTF.text = self.tempStr;
            
        }
            break;
        case 2:
        {
            self.tempStr = self.confirmNewPswTF.text;
            self.confirmNewPswTF.text = @"";
            self.confirmNewPswTF.secureTextEntry = !self.confirmNewPswTF.secureTextEntry;
            self.confirmNewPswTF.text = self.tempStr;
        }
            break;
        case 3:
        {
            self.tempStr = self.PswTF.text;
            self.PswTF.text = @"";
            self.PswTF.secureTextEntry = !self.PswTF.secureTextEntry;
            self.PswTF.text = self.tempStr;
        }
            break;
        default:
            break;
    }
    
}
- (void)AlterLoginPassword
{
    if ([self.oldPswTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入旧密码" message:nil waitingTime:1.5];
    }else if ([self.PswTF.text isEqualToString:@""]){
        [GUATool showDialog:@"请输入新密码" message:nil waitingTime:1.5];
    }else if (![CommonUtil validatePassword:self.PswTF.text]){
        [GUATool showDialog:@"登录密码为6-16位的字母与数字的组合" message:nil waitingTime:1.5];
    }else if ([self.confirmNewPswTF.text isEqualToString:@""]){
        [GUATool showDialog:@"请再次输入新密码" message:nil waitingTime:1.5];
    }else if (![self.PswTF.text isEqualToString:self.confirmNewPswTF.text]){
        [GUATool showDialog:@"两次输入的密码不一样" message:nil waitingTime:1.5];
    }else if ([self.oldPswTF.text isEqualToString:self.PswTF.text]){
        [GUATool showDialog:@"新旧密码不能相同" message:nil waitingTime:1.5];
    } else{
        [KJSRequest AlterLoginPasswordWithOldPw:self.oldPswTF.text newpw:self.PswTF.text phoneNumber:CurentDelegate.account.phoneNumber ret:^(BasicResult *model) {
            if ([model.returnMsg isEqualToString:@"success"]) {
                [GUATool showDialog:@"修改成功" message:nil waitingTime:1.5];
                [self dismiss:YES];
            }
        }];
    }
}

@end
